import 'dart:typed_data';

import 'package:bluetooth_protocols_plugin/src/huari/message/huari_request_body.dart';
import '../../../tools/utils.dart';
import '../../base/cmd/huari_cmd_base.dart';

// Huari私有请求体
abstract class HuariPrivateRequestBody<C extends HuariCmdBase> {
  // 基础指令
  C get cmdBase;

  // 获取随机码
  HuariRequestBody getAuthRandomCode(Uint8List identifierBytes) =>
      HuariRequestBody(
        'GetAuthRandomCode',
        cmdBase.getAuthRandomCode.$1,
        param: identifierBytes,
      );

  // 验证随机码
  HuariRequestBody verifyAuthRandomCode(Uint8List bytes) => HuariRequestBody(
        'VerifyAuthRandomCode',
        cmdBase.verifyAuthRandomCode.$1,
        param: bytes,
      );

  // 锁车/解锁
  HuariRequestBody lock(Uint8List identifyBytes, bool status) =>
      HuariRequestBody(
        'Lock',
        cmdBase.lock.$1,
        param: Uint8List.fromList([
          ...identifyBytes,
          ...(status ? [0x00] : [0x01])
        ]),
      );

  // 写入产品模型
  HuariRequestBody writeProductModel(Uint8List bytes) => HuariRequestBody(
        'WriteProductModel',
        cmdBase.writeProductModel.$1,
        param: bytes,
      );

  // 写入激活时间
  HuariRequestBody writeActiveTime(Uint8List time) {
    return HuariRequestBody(
      "WriteActiveTime",
      cmdBase.writeActiveTime.$1,
      param: time,
    );
  }

  // nfc配卡 - 进入NFC配卡模式
  HuariRequestBody enterNfcCardMode() => HuariRequestBody(
        'EnterNfcCardMode',
        cmdBase.enterNfcCardMode.$1,
      );

  // nfc配卡 - 退出NFC配卡模式
  HuariRequestBody exitNfcCardMode() => HuariRequestBody(
        'ExitNfcCardMode',
        cmdBase.exitNfcCardMode.$1,
      );

  // nfc配卡 - 删除NFC卡
  HuariRequestBody deleteNfcCard() => HuariRequestBody(
        'DeleteNfcCard',
        cmdBase.deleteNfcCard.$1,
      );

  // 蓝牙钥匙 - 添加主密钥
  HuariRequestBody addMasterKey(Uint8List bytes) => HuariRequestBody(
        'AddMasterKey',
        cmdBase.addMasterKey.$1,
        param: bytes,
      );

  // 蓝牙钥匙 - 删除主密钥
  HuariRequestBody deleteMasterKey(Uint8List bytes) => HuariRequestBody(
        'DeleteMasterKey',
        cmdBase.deleteMasterKey.$1,
        param: bytes,
      );

  // 蓝牙钥匙 - 添加子密钥
  HuariRequestBody addSubKey(Uint8List bytes) => HuariRequestBody(
        'AddSubKey',
        cmdBase.addSubKey.$1,
        param: bytes,
      );

  // 蓝牙钥匙 - 删除子密钥
  HuariRequestBody deleteSubKey(Uint8List bytes) => HuariRequestBody(
        'DeleteSubKey',
        cmdBase.deleteSubKey.$1,
        param: bytes,
      );

  // 蓝牙钥匙 - 获取密钥列表
  HuariRequestBody getKeyList(Uint8List bytes) => HuariRequestBody(
        'GetKeyList',
        cmdBase.getKeyList.$1,
        param: bytes,
      );

  // 获取产品模型
  HuariRequestBody get getProductModel => HuariRequestBody(
        'GetProductModel',
        cmdBase.getProductModel.$1,
      );

  // 获取sn信息
  HuariRequestBody get getSN => HuariRequestBody(
        'GetSN',
        cmdBase.getSn.$1,
      );

  // 设置骑行模式
  HuariRequestBody setRideMode(int mode) {
    return HuariRequestBody(
      'SetRideMode',
      cmdBase.setRideMode.$1,
      param: Utils.int2Uint8List(mode),
    );
  }

  // 开启周期数据开关
  HuariRequestBody enablePeriodicDataSwitch(bool enable) {
    return HuariRequestBody(
      'EnablePeriodicDataSwitch',
      cmdBase.enablePeriodicDataSwitch.$1,
      param: Utils.int2Uint8List(enable ? 0 : 1),
    );
  }

  // 读取控制数据
  HuariRequestBody getControllerData(Uint8List addrBytes) {
    return HuariRequestBody(
      'getControllerData',
      cmdBase.getControllerData.$1,
      param: addrBytes,
    );
  }

  // 设置控制数据
  HuariRequestBody setControllerData(Uint8List addrBytes, Uint8List dataBytes) {
    return HuariRequestBody(
      'SetControllerData',
      cmdBase.setControllerData.$1,
      param: Uint8List.fromList([...addrBytes, ...dataBytes]),
    );
  }
}
